<!--#
layout("/layouts/platform.html"){
#-->
<div id="app" v-cloak>
    <el-row class="header navbar bg-white shadow">
        <div class="btn-group tool-button mt5">
            <el-date-picker
                    v-model="searchDate"
                    type="datetimerange"
                    :picker-options="pickerOptions"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    align="right"
                    size="medium"
                    :default-time="['00:00:00', '23:59:59']"
                    value-format="yyyy-MM-dd HH:mm:ss"
            >
            </el-date-picker>
        </div>
        <!--按赛事类型搜索-->
        <div class="btn-group tool-button mt5">
            <el-select v-model="pageForm.matchType" placeholder="请选择赛事类型"
                       size="medium"
            >
                <el-option
                        v-for="item in matchTypeOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                >
                </el-option>
            </el-select>
        </div>
        <!--按赛事阶段搜索-->
        <div class="btn-group tool-button mt5">
            <el-select v-model="pageForm.stage" placeholder="请选择赛事阶段"
                       size="medium"
            >
                <el-option
                        v-for="item in matchStageOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                >
                </el-option>
            </el-select>
        </div>
        <!--按联赛或冠军名搜索-->
        <div class="btn-group tool-button mt5">
            <el-input v-model="pageForm.searchKeyword" @keyup.enter.native="doSearch" placeholder="联赛、冠军名"
                      size="medium"></el-input>
        </div>
        <!--搜索按钮-->
        <div class="btn-group tool-button mt5">
            <el-button slot="append" icon="el-icon-search" @click="doSearch" size="medium"></el-button>
        </div>
        <!--爬取指定日期赛果-->
        <div class="pull-right offscreen-right mt5">
            <el-button size="medium" @click="crawlLeagueResult" title="(重新)获取指定时间区间内的赛果">获取赛果</el-button>
        </div>
    </el-row>
    <el-row class="el-table-container">
        <el-table
                :data="tableData"
                size="small"
                header-align="center"
                style="width: 100%"
        >
            <el-table-column
                    prop="league"
                    label="所属联赛"
                    header-align="center"
                    :show-overflow-tooltip="true"
                    align="center">
            </el-table-column>

            <el-table-column
                    prop="championName"
                    label="冠军名称"
                    header-align="center"
                    :show-overflow-tooltip="true"
                    align="center">
            </el-table-column>

            <el-table-column
                    sortable
                    prop="type"
                    label="比赛类型"
                    header-align="center"
                    align="center">
                <template slot-scope="scope">
                    <span v-if="scope.row.type=='BASKETBALL'">篮球</span>
                    <span v-else>足球</span>
                </template>
            </el-table-column>
            <el-table-column
                    sortable
                    label="阶段"
                    header-align="center"
                    align="center">
                <template slot-scope="scope">
                    <span v-if="scope.row.stage==null">待补充</span>
                    <span v-else-if="scope.row.stage=='running'">进行中</span>
                    <span v-else-if="scope.row.stage=='finished'">已结束</span>
                </template>
            </el-table-column>
            <el-table-column
                    sortable
                    prop="dateTime"
                    label="时间"
                    header-align="center"
                    align="center">
            </el-table-column>
            <el-table-column
                    sortable
                    prop="winner"
                    label="冠军"
                    header-align="center"
                    align="center">
            </el-table-column>

            <el-table-column
                    label="操作"
                    width="120"
                    align="center">
                <template slot-scope="scope">
                    <el-dropdown @command="dropdownCommand">
                        <el-button size="mini">
                            <i class="ti-settings"></i>
                            <span class="ti-angle-down"></span>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item divided :command="{type:'edit',id:scope.row.id}">
                                修改
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>
        </el-table>
    </el-row>
    <!--分页容器-->
    <el-row class="el-pagination-container">
        <el-pagination
                @size-change="pageSizeChange"
                @current-change="pageNumberChange"
                :current-page="pageForm.pageNumber"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="pageForm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageForm.totalCount">
        </el-pagination>
    </el-row>
    <!--查看赛果详情dialog-->
    <el-dialog
            title="赛果详情"
            :visible.sync="editDialogVisible"
            :close-on-click-modal="false"
            width="40%">
        <el-form :model="formData" :inline="true" ref="editForm" :rules="formRules" size="small" label-width="80px">
            <el-form-item prop="stage" label="阶段">
                <el-select v-model="formData.stage" placeholder="阶段"
                           size="medium"
                >
                    <el-option
                            key="running"
                            label="进行中"
                            value="running"></el-option>
                    <el-option
                            key="finished"
                            label="已结束"
                            value="finished"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="查看版本">
                <el-select v-model="version" placeholder="阶段" size="medium" @change="changeVersion" style="width: 60px"
                >
                    <el-option
                            v-for="item in versionObj"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <!-- 强制解锁修改赛果-->
            <el-button @click="unlock()" type="warning" size="medium" icon="el-icon-lock"></el-button>
            <el-form-item prop="winner" label="赛果" v-for="(item,index) in formDataMatch"
                          v-if="formDataMatch.length > 0" style="width: 100%">
                <el-input maxlength="100"
                          v-model="item.winner"
                          auto-complete="off"
                          tabindex="1"
                          type="text" style="width: 220px"
                          :disabled="true">
                </el-input>
                <!--删除input框-->
                <el-button type="danger" icon="el-icon-delete" @click="delMatch(index)" v-if="index > 0"></el-button>
                <!--增加input框-->
                <el-button @click="addMatch()" type="primary" icon="el-icon-plus"
                           v-if="index == (formDataMatch.length - 1)"></el-button>
            </el-form-item>
        </el-form>
        <!--        确认/取消 提交修改按钮-->
        <span slot="footer" class="dialog-footer">
            <el-button @click="editDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="doEdit">结 算</el-button>
        </span>
    </el-dialog>
</div>
<script src="${base!}/assets/platform/js/sha256.js"></script>
<script>
    new Vue({
        el: "#app",
        data: function () {
            var self = this;
            return {
                matchTypeOptions: [{
                    value: '',
                    label: '全部类型'
                }, {
                    value: 'FOOTBALL',
                    label: '足球'
                }, {
                    value: 'BASKETBALL',
                    label: '篮球'
                }],
                matchStageOptions: [{
                    value: '',
                    label: '全部阶段'
                }, {
                    value: 'running',
                    label: '进行中'
                }, {
                    value: 'finished',
                    label: '已结束'
                }, {
                    value: 'other',
                    label: '其他'
                }],
                pickerOptions: {
                    disabledDate: function (time) {
                        var threeMonths = moment().add(-3, 'months').valueOf();
                        return time.getTime() > new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1)
                            || time.getTime() < threeMonths;
                    },
                    shortcuts: [{
                        text: '最近一周',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'weeks').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-3, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                searchDate: [],
                addDialogVisible: false,
                editDialogVisible: false,
                titlePopu: 0,
                tableData: [],
                pageForm: {
                    pageNumber: 1,
                    pageSize: 10,
                    totalCount: 0,
                    searchDate: "",
                    matchType: "",
                    stage: "",
                },
                formData: {
                    stage: "future",
                },
                formDataMatch: [
                    {
                        leagueId: '',
                        winner: '',
                    }
                ],
                version: 0,
                versionObj: [], //所有版本
                formRules: {
                    // 新增User昵称必须填写。修改User暂时没有修改昵称功能。
                    // nickName: [
                    //     {required: true, message: '请输入昵称', trigger: 'blur'}
                    // ],
                }
            }
        },
        methods: {
            //删除赛果
            delMatch: function (index) {
                this.formDataMatch.splice(index, 1);
            },
            //增加赛果
            addMatch: function () {
                let self = this;
                this.formDataMatch.push({
                    leagueId: self.formData.id,
                    winner: '',
                });
            },
            pageNumberChange: function (val) {//页码更新操作
                this.pageForm.pageNumber = val;
                this.pageData();
            },
            pageSizeChange: function (val) {//分页大小更新操作
                this.pageForm.pageSize = val;
                this.pageData();
            },
            pageData: function () {//加载分页数据
                var self = this;
                sublime.showLoadingbar();//显示loading
                if (self.searchDate) {
                    self.pageForm.searchDate = self.searchDate.toString();
                } else {
                    self.pageForm.searchDate = "";
                }
                self.pageForm.start = self.pageForm.searchDate.split(',')[0];
                self.pageForm.end = self.pageForm.searchDate.split(',')[1];
                $.post(base + "/platform/crown/league/data", self.pageForm, function (data) {
                    sublime.closeLoadingbar();//关闭loading
                    if (data.code == 0) {
                        self.tableData = data.data.list;
                        self.pageForm.totalCount = data.data.totalCount;
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            doSearch: function () {
                this.pageForm.pageNumber = 1;
                this.pageData();
            },
            // 爬取赛果按钮
            crawlLeagueResult: function () {
                var self = this;
                sublime.showLoadingbar();//显示loading
                if (self.searchDate.length === 0) {
                    self.$message({
                        message: "请选择要爬取的日期",
                        type: 'error'
                    });
                    return;
                }
                self.pageForm.searchDate = self.searchDate.toString();
                $.post(base + "/platform/crown/league/crawlLeagueResult", self.pageForm, function (data) {
                    if (data.code === 0) {
                        self.$message({
                            message: data.msg,
                            type: 'success'
                        });
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            openAdd: function () {// 修改按钮
                var self = this;
                self.titlePopu = 0;
                this.editDialogVisible = true;
                self.formDataMatch = [{
                    winner: ''
                }];
                if (this.$refs["editForm"]) {
                    this.$refs["editForm"].resetFields();
                }
            },
            doEdit: function () {
                var self = this;
                self.$refs["editForm"].validate(function (valid) {
                    if (valid) {
                        self.formData.subResults = JSON.stringify(self.formDataMatch);
                        // 修改成功请求结算接口
                        $.post(base + "/platform/crown/league/settleChampion", self.formData, function (data) {
                            if (data.code == 0) {
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                        self.doSearch();
                        self.editDialogVisible = false;
                    }
                });
            },
            dropdownCommand: function (command) {//监听下拉框事件
                var self = this;
                // 修改按钮
                if ("edit" == command.type) {
                    let params = command.id;
                    if (command.remark && command.remark === 'changeVersion') {
                        params += '&oldVersion=' + command.oldVersion;
                    }
                    $.post(base + "/platform/crown/league/edit?id=" + params, {}, function (data) {
                        if (data.code == 0) {
                            if (self.$refs["editForm"]) {// 清空校验提示
                                self.$refs["editForm"].resetFields();
                            }
                            self.formData = data.data;// 加载后台表单数据
                            if (!command.remark) {
                                self.version = data.data.version;
                            }
                            self.versionObj = [];
                            for (let i = 0; i < data.data.version + 1; i++) {
                                let obj = {
                                    id: i,
                                    name: i
                                };
                                self.versionObj.push(obj);
                            }
                            if (data.data.subResults.length > 0) {
                                self.formDataMatch = data.data.subResults;
                            }
                            self.formDataMatch[0].leagueId = data.data.id;
                            self.editDialogVisible = true;// 打开编辑窗口
                            self.titlePopu = 1;
                        } else {
                            self.$message({
                                message: data.msg,
                                type: 'error'
                            });
                        }
                    }, "json");
                }
            },
            changeVersion: function () {
                let command = {};
                command.type = 'edit';
                command.remark = 'changeVersion';
                command.id = this.formData.id;
                command.oldVersion = this.version;
                this.dropdownCommand(command);
            }
        },
        created: function () {
            this.pageData();
        }
    });
</script>

<!--#
}
#-->